import 'package:flutter_todolist/db/table/todo_table.dart';
import 'package:flutter_todolist/utils/logger.dart';
import 'package:sqflite_common/sqlite_api.dart';

class UserTable implements TodoTable {
  @override
  Database? database;

  final String userTable = "user";

  @override
  createTable() {
    return '''
      CREATE TABLE IF NOT EXISTS $userTable (
      userId TEXT PRIMARY KEY,
      name TEXT,
      avatar TEXT,
      age INTEGER,
      gender INTEGER,
      account TEXT,
      password TEXT
      )
    ''';
  }

  @override
  deleteItem(id) async {
    final Database db = database!;
    try {
      await db.delete(
          userTable,
          where: "userId = ?",
          whereArgs: [id]
      );
      logW("taskTable删除成功");
    } catch (e){
      logE("$userTable删除出错: $e");
    }
  }

  @override
  Future insertItem(Map<String, Object?> item) async {
    final Database db = database!;
    try {
      await db.insert(userTable, item);
      logW("taskTable插入成功");
    } catch (e){
      logE("插入出错: $e");
    }
  }

  // List<Map<String, dynamic>> test = await testTable.queryList(
  //   where: "mid = ?",
  //   whereArgs: [widget.mid],
  // );
  @override
  Future<List<Map<String, dynamic>>> queryList({
    bool distinct = true, // 是否是独特的，即是否不让重复
    List<String>? columns, // 需要查询的列
    String? where, // 查询条件 "age > ?"
    List<Object>? whereArgs, // 查询条件参数(补充where) [16]
    String? groupBy, // 按列分组
    String? having, // 给分组设置条件
    String? orderBy, // 按列排序 asc/desc
    int limit = 100, // 限制查询结果数量
    int offset = 0, // 跳过几条数据
  }) async {
    final Database db = database!;
    try {
      List<Map<String, dynamic>> result = await db.query(
        userTable,
        distinct: distinct,
        columns: columns,
        where: where,
        whereArgs: whereArgs,
        groupBy: groupBy,
        having: having,
        orderBy: orderBy,
        limit: limit,
        offset: offset,
      );
      return result;
    } catch (e){
      logE("查询出错: $e");
    }

    return [];
  }

  @override
  updateItem(userId, item) async {
    final Database db = database!;
    try {
      await db.update(
          userTable,
          item,
          where: "userId = ?",
          whereArgs: [userId]
      );
      logW("taskTable更新成功");
    } catch (e){
      logE("$userTable更新出错: $e");
    }
  }

}


class User {
  User({
    this.userId,
    this.name,
    this.avatar,
    this.age,
    this.gender,
    this.account,
    this.password
  });

  User.fromJson(dynamic json) {
    userId = json['userId'];
    name = json['name'];
    avatar = json['avatar'];
    age = json['age'];
    gender = json['gender'];
    account = json['account'];
    password = json['password'];
  }

  String? userId;
  String? name;
  String? avatar;
  int? age;
  int? gender;
  String? account;
  String? password;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['userId'] = userId;
    map['name'] = name;
    map['avatar'] = avatar;
    map['age'] = age;
    map['gender'] = gender;
    map['account'] = account;
    map['password'] = password;

    return map;
  }

}